package web;

import com.google.gson.Gson;
import pojo.Goods;
import pojo.Store;
import service.StoreService;
import service.impl.StoreServiceImpl;
import utils.WebUtils;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lq
 * @create 2021/1/10
 */
@WebServlet(name = "StoreServlet")
public class StoreServlet extends BaseServlet {
    private StoreService storeService = new StoreServiceImpl();

    /**
     * 添加仓库
     * @param request store(至少要id,ownerId,name,updateTime)
     * @param response 1表示添加成功，0表示添加失败
     */
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Store store = WebUtils.copyParamToBean(request.getParameterMap(),new Store());
        Map<String, Integer> map = new HashMap<>(16);
        if(storeService.addStore(store)){
            map.put("status", 1);
        }else{
            map.put("status", 0);
        }

        Gson gson = new Gson();
        String userJson = gson.toJson(map);
        response.getWriter().print(userJson);
    }

    /**
     * 根据仓库的 id 删除仓库
     * @param request 仓库的id
     * @param response 1为删除成功，0为删除失败
     */
    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Map<String, Integer> map = new HashMap<>(16);
        if(storeService.deleteStore(id)){
            map.put("status", 1);
        }else{
            map.put("status", 0);
        }
        Gson gson = new Gson();
        String userJson = gson.toJson(map);
        response.getWriter().print(userJson);
    }

    /**
     * 根据用户 ownerId 查找他所拥有的所有仓库
     * @param request ownerId
     * @param response 仓库列表
     */
    protected void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer ownerId = Integer.parseInt(request.getParameter("ownerId"));
        Gson gson = new Gson();
        List<Store> stores = storeService.queryStoresByOwnerId(ownerId);
        String userJson = null;
        if(stores!=null){
            userJson = "{\"status\":1,\"info\":" + gson.toJson(stores)+"}";
        }else{
            userJson = "{\"status\":0}";
        }
        response.getWriter().print(userJson);
        //System.out.println(stores);
    }

    /**
     * 更新仓库信息
     * @param request store(至少要id,ownerId,name,updateTime)
     * @param response  1为更新成功，0为更新失败
     */
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Store store = WebUtils.copyParamToBean(request.getParameterMap(),new Store());
        Map<String, Integer> map = new HashMap<>(16);
        if(storeService.updateStore(store)){
            map.put("status", 1);
        }else{
            map.put("status", 0);
        }
        Gson gson = new Gson();
        String userJson = gson.toJson(map);
        response.getWriter().print(userJson);
    }
}
